#include "bruitpoivreetsel.h"
#include "pimage.h"
#include <iostream>

using namespace std;

BruitPoivreEtSel::BruitPoivreEtSel(QImage* image, PImage* pimage):QDialog()
{
    this->image = image;
    this->pimage = pimage;

    pourcentage=0;

     ui_Bruit = new Ui_BruitPS::Bruit();
     QDialog* dialogBruit;
     dialogBruit = new QDialog(pimage);
     ui_Bruit->setupUi(dialogBruit);
     dialogBruit->show();


     QObject::connect(ui_Bruit->spinBox, SIGNAL(valueChanged(int)), this, SLOT(getPourcentage(int)));
     QObject::connect(ui_Bruit->dialButton, SIGNAL(valueChanged(int)), this, SLOT(getPourcentage(int)));
     QObject::connect(ui_Bruit->buttonBox, SIGNAL(accepted()), this, SLOT(updateBruit()));
     QObject::connect(ui_Bruit->buttonBox, SIGNAL(accepted()), pimage->getCurrentDrawing(), SLOT(update()));


}

void BruitPoivreEtSel::getPourcentage(int pourcentage){
    this->pourcentage = pourcentage;
}

void BruitPoivreEtSel::updateBruit(){


    int coordxN ;
    int coordyN ;
    int coordxB ;
    int coordyB ;
    int w = image->width();
    int h = image->height();


    float nbpixels = ((float)(w * h * pourcentage)) /(2*100);

    if(pourcentage==0){
    }else{
        for(int i =0 ;i < nbpixels ; i++){

            coordxN = rand()%(w);
            coordyN = rand()%(h);
            coordxB = rand()%(w);
            coordyB = rand()%(h);

            image->setPixel(coordxN,coordyN,qRgb(0,0,0));
            image->setPixel(coordxB, coordyB, qRgb(255,255,255));

        }
    }
    pimage->updateCalque();
}
